package cn.szabadsag.pet.controller;

import cn.szabadsag.pet.domain.Pet;
import cn.szabadsag.pet.query.PetQuery;
import cn.szabadsag.pet.service.IPetService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;


    /**
     * 根据id查询单个宠物信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Pet queryPage(@PathVariable Long id){
        return petService.loadByIdDetail(id);
    }


    /**
     * 高级查询+分页
     * @param petQuery
     * @return
     */
    @PostMapping
    public QueryResult<Pet> queryPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    /**
     * 宠物领养下架
     * @param list
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> list){
        try {
            petService.offsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 宠物领养上架
     * @param list
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> list){
        try {
            petService.onsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 新增宠物领养
     */
    @PutMapping
    public AjaxResult save(@RequestBody Pet pet){
        try {
            petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }



}
